﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Lion.Data;
using Forest.Lion.Model;

namespace Forest.Lion.Domain.Services
{
    public class LovHandler
    {
        private readonly ILovRepository _lovRepository;
        private static List<Lov> _lovList;

        #region Property
        private IEnumerable<Lov> LovList
        {
            get
            {
                if (_lovList == null)
                {
                    _lovList = _lovRepository.GetAll().ToList();
                }
                return _lovList;
            }
        }
        #endregion

        public LovHandler(ILovRepository lovRepository)
        {
            this._lovRepository = lovRepository;
        }

        public Lov GetById(Guid lovId)
        {
            return LovList.FirstOrDefault(x => x.Id.Equals(lovId));
        }

        public IEnumerable<Lov> GetListByParentId(Guid parentId)
        {
            return LovList.Where(x => x.ParentId.Equals(parentId))
                .OrderBy(x => x.SortOrder).ToList();
        }

        public IEnumerable<Lov> GetSubListByParentId(Guid parentId)
        {
            var lovIdList = GetListByParentId(parentId).Select(x => x.Id);
            return LovList.Where(x => lovIdList.Contains(x.ParentId ?? Guid.Empty)).ToList();
        }
    }
}
